package com.etoak.service;

import cn.hutool.json.JSONUtil;
import com.etoak.common.vo.Email;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Value("${spring.mail.username}")
    String senderAddress;

    /**
     * 邮件发送对象
     */
    @Autowired
    JavaMailSender javaMailSender;

    @RabbitListener(queues = "email")
    public void send(String msg) {
        System.out.println("收到消息:" + msg);

        Email email = JSONUtil.toBean(msg, Email.class);

        /* 创建邮件消息 */
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 发件人
        mailMessage.setFrom(this.senderAddress);
        // 收件人 主送
        mailMessage.setTo(email.getReceiver());
        // 抄送
        mailMessage.setCc("3044683361@qq.com");
        // 主题
        mailMessage.setSubject(email.getSubject());
        // 邮件正文
        mailMessage.setText(email.getContent());

        System.out.println("开始发送邮件");
        javaMailSender.send(mailMessage);
        System.out.println("发送结束");
    }


}
